# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import csv
from utils.HttpUtils import HttpUtils


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
ins
https://www.instagram.com
通过搜索关键字，提取图片、用户信息
phelpslangworthxqej60@gmail.com 
testuser6666!


testuser87878@gmail.com
87878testuser

15216788642
123456Aa
"""


class Ins_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "authority": "www.instagram.com",
            "method": "GET",
            "path": '/graphql/query/?query_hash=90cba7a4c91000cf16207e4f3bee2fa2&variables=%7B%22tag_name%22%3A%22%E3%83%8D%E3%82%A4%E3%83%AB%E3%83%87%E3%82%B6%E3%82%A4%E3%83%B3%22%2C%22first%22%3A11%2C%22after%22%3A%22QVFEZW1zNDVla1FZNk44MVJFa25Rd0RmdVR5YVRtUk9OWkJIdzJPbnhBQndDY0lKZ2dWbm5MVkpJUzVIazJRX2N3bkZQRmVoajBDSUQydDhoYU5WZElLQg%3D%3D%22%7D',
            "scheme": "https",
            "accept": "*/*",
            "accept-encoding": "gzip, deflate, br",
            "accept-language": "zh-CN,zh;q=0.9",
            "cookie": 'ig_did=4CCF43CF-2B72-41E0-9519-DC6DC39225F1; rur=FTW; mid=Xg2X-QALAAGLWyseuS6fh8sDp6-D; csrftoken=GvWdie3cmA3sy9868ZzkfzAXy7SY2buY; ds_user_id=27402765278; sessionid=27402765278%3Ax1QILd7EGeUwnx%3A1; urlgen="{\"23.98.35.120\": 8075\054 \"112.120.52.223\": 4760\054 \"45.93.217.93\": 9009}:1imzNE:q9a4b_8AnAU-_IMC7n6Tdq6vhqM"',
            "dnt": "1",
            "referer": 'https://www.instagram.com/explore/tags/%E3%83%8D%E3%82%A4%E3%83%AB%E3%83%87%E3%82%B6%E3%82%A4%E3%83%B3/',
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "x-csrftoken": "GvWdie3cmA3sy9868ZzkfzAXy7SY2buY",
            "x-ig-app-id": "936619743392459",
            "x-ig-www-claim": "hmac.AR1tt59b1rqDb_f8E62WT4dQvUIpVUBT1usxHVSPWYSJ5v-b",
            "x-requested-with": "XMLHttpRequest",

            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
        }

    # 搜索关键词内容
    def get_contents(self, keyword, parm):
        try:
            info_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            filename = datetime.now().strftime("%Y-%m-%d")

            # parm = "QVFEZW1zNDVla1FZNk44MVJFa25Rd0RmdVR5YVRtUk9OWkJIdzJPbnhBQndDY0lKZ2dWbm5MVkpJUzVIazJRX2N3bkZQRmVoajBDSUQydDhoYU5WZElLQg=="
            url = f'https://www.instagram.com/graphql/query/?query_hash=90cba7a4c91000cf16207e4f3bee2fa2&variables={{"tag_name":"{keyword}","first":12,"after":"{parm}"}}'
            html = HttpUtils.do_request("GET", url, self.headers, "")
            data_json = json.loads(html.text)
            if data_json['status'] == "ok":
                # 下一页游标
                end_cursor = data_json['data']['hashtag']['edge_hashtag_to_media']['page_info']['end_cursor']
                # 是否有下一页 True
                has_next_page = data_json['data']['hashtag']['edge_hashtag_to_media']['page_info']['has_next_page']
                for d in data_json['data']['hashtag']['edge_hashtag_to_media']['edges']:
                    dict_data = dict()
                    shortcode = d['node']['shortcode']  # 图片短码
                    dict_data['is_video'] = d['node']['is_video']  # 是否是视频
                    dict_data['display_url'] = d.get('node').get('display_url')  # 是否是视频
                    dict_data['like_count'] = d['node']['edge_liked_by']['count']  # 点赞数量
                    dict_data['comments_count'] = d['node']['edge_media_to_comment']['count']  # 评论数量
                    print(f"=========解析{shortcode}===========")

                    self.get_pic("CIHWlrgDzhj", dict_data, parm)

                if has_next_page:
                    print("========下一页=========")
                    return self.get_contents(end_cursor)
                else:
                    end_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                    filename = datetime.now().strftime("%Y-%m-%d")
                    print("========结束=========")
        except Exception as ex:
            filename = datetime.now().strftime("%Y-%m-%d")
            error_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            with open("error_" + filename + ".txt", 'a', encoding="utf-8") as f:
                f.write("==========================================" + "\n")
                f.write(error_time + "  " + str(ex) + "\n")
                f.write("当前页: " + parm + "\n")

    # 获取该链接下的图片
    def get_pic(self, shortcode, dict_data, parm):
        headers = {
            "authority": "www.instagram.com",
            "method": "GET",
            "path": '/graphql/query/?query_hash=109cdd03d7468e12222ad164fbea3ca3&variables=%7B%22shortcode%22%3A%22B60HiLGJ8QX%22%2C%22child_comment_count%22%3A3%2C%22fetch_comment_count%22%3A40%2C%22parent_comment_count%22%3A24%2C%22has_threaded_comments%22%3Atrue%7D',
            "scheme": "https",
            "accept": "*/*",
            "accept-encoding": "gzip, deflate, br",
            "accept-language": "zh-CN,zh;q=0.9",
            # "cookie": 'ig_did=4CCF43CF-2B72-41E0-9519-DC6DC39225F1; rur=FTW; mid=Xg2X-QALAAGLWyseuS6fh8sDp6-D; csrftoken=GvWdie3cmA3sy9868ZzkfzAXy7SY2buY; ds_user_id=27402765278; sessionid=27402765278%3Ax1QILd7EGeUwnx%3A1; urlgen="{\"23.98.35.120\": 8075\054 \"112.120.52.223\": 4760\054 \"45.93.217.93\": 9009}:1imzNE:q9a4b_8AnAU-_IMC7n6Tdq6vhqM"',
            "dnt": "1",
            "referer": 'https://www.instagram.com/explore/tags/%E3%83%8D%E3%82%A4%E3%83%AB%E3%83%87%E3%82%B6%E3%82%A4%E3%83%B3/',
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "x-csrftoken": "GvWdie3cmA3sy9868ZzkfzAXy7SY2buY",
            "x-ig-app-id": "936619743392459",
            "x-ig-www-claim": "hmac.AR1tt59b1rqDb_f8E62WT4dQvUIpVUBT1usxHVSPWYSJ5v-b",
            "x-requested-with": "XMLHttpRequest",

            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
        }
        try:
            url = 'https://www.instagram.com/graphql/query/?query_hash=109cdd03d7468e12222ad164fbea3ca3&variables={"shortcode":"'+ shortcode + '","child_comment_count":3,"fetch_comment_count":40,"parent_comment_count":24,"has_threaded_comments":true}'
            html = HttpUtils.do_request("GET", url, headers, "")
            data_json = json.loads(html.text)
            if data_json['status'] == "ok":
                # 用户名称
                dict_data['username'] = data_json['data']['shortcode_media']['owner']['username']
                create_time = data_json['data']['shortcode_media']['taken_at_timestamp']
                dict_data['create_time'] = datetime.fromtimestamp(int(create_time)).strftime("%Y-%m-%d %H:%M:%S")
                display_url = dict_data['display_url']  # 缩略图第一个
                picList1080 = []  # 1080格式链接
                if data_json['data']['shortcode_media'].get('edge_sidecar_to_children') is not None:
                    for p in data_json['data']['shortcode_media']['edge_sidecar_to_children']['edges']:
                        picList1080.append(p['node']['display_url'])

                # picList640 = []  # 640链接
                # for p in data_json['data']['shortcode_media']['display_resources']:
                #     picList640.append(p['src'])

                # 获取用户详细信息
                # url = "https://www.instagram.com/" + dict_data['username']
                # html = HttpUtils.do_request("GET", url, headers, "")
                # root = etree.HTML(html.text)
                # dianzan = root.xpath('//*[@name="description"]/@content')[0]
                # tmp = dianzan.split("-")
                # 获取用户详细信息
                # 下载图片
                savePath = "./pic/"
                downPicList = []
                if len(picList1080) > 0:
                    downPicList = picList1080
                else:
                    downPicList.append(display_url)
                num = 0
                for pic in downPicList:
                    num += 1
                    pic_content = requests.get(pic, headers)
                    with open(savePath + dict_data['username'] + "_" + str(num) + ".jpg", 'wb') as f:
                        f.write(pic_content.content)

                print(dict_data['username'])
            else:
                print("========={username}============")
        except Exception as ex:
            filename = datetime.now().strftime("%Y-%m-%d")
            error_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            with open("error_" + filename + ".txt", 'a', encoding="utf-8") as f:
                f.write("==========================================" + "\n")
                f.write(error_time + "  " + str(ex) + "\n")
                f.write("当前页: " + parm + "\n")


if __name__ == '__main__':
    ins_get = Ins_Get()
    parm = "QVFEU29UQ0NCSFlpMTR1N0ZqcUlaRTdZRnFudnJvZEhoNDVYMDFYVFBsXzd2WnZLUjFNNlZBdWIya3l0Rk1pc1hwSW9mOUJLV2RSeEY5THVqSDMxcFdDdA=="
    ins_get.get_contents("ネイルデザイン", parm)
    # test_get.get_username("B60OQr7JY_c")